package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.Checkitem;
import com.itheima.exception.BussinessException;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.message.BussinessErrorMsg;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
@Service
@Transactional
public class CheckitemServiceImpl extends ServiceImpl<CheckItemMapper, Checkitem>
        implements CheckItemService {

    @Autowired
    private CheckGroupItemService cm;

    @Autowired
    private CheckItemMapper mapper;

    @Autowired
    private CheckGroupItemService checkGroupItemService;

    // 新增
    @Override
    public void add(Checkitem checkitem) {
        mapper.insert(checkitem);
    }

    // 删除
    @Override
    public void deleteById(Integer id) {
        boolean checkItemExisted = cm.checkItemExisted(id);
        if (!checkItemExisted) {
            mapper.deleteById(id);
        } else {
            BussinessException
                    .error(BussinessErrorMsg.DELETE_CHECKITEM_FAIL.concat(", 存在关联检查组"));
        }
    }

    // 详情
    @Override
    public Checkitem findOne(Integer id) {
        Checkitem checkitem = mapper.selectById(id);
        return checkitem;
    }

    // 更新
    @Override
    public void update(Checkitem checkitem) {
        QueryWrapper<Checkitem> wrapper = new QueryWrapper<>();
        Integer id = checkitem.getId();
        wrapper.eq("id", id);
        mapper.update(checkitem, wrapper);
    }

    // 分页查询
    @Override
    public PageResult<Checkitem> selectPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        Page<Checkitem> page = new Page<>(currentPage, pageSize);

        //        boolean empty = StringUtils.isEmpty(queryString);

        boolean flag = (queryString != null && queryString != "");

        LambdaQueryWrapper<Checkitem> queryWrapper =
                Wrappers.lambdaQuery(Checkitem.class);
        queryWrapper.likeRight(flag, Checkitem::getCode, queryString).or()
                .likeRight(flag, Checkitem::getName, queryString);

        //        QueryWrapper<Checkitem> queryWrapper = new QueryWrapper<>();
        //        queryWrapper.likeRight(flag,"name",queryString)
        //                .or()
        //                .likeRight(flag,"code",queryString);

        Page<Checkitem> checkitemIPage = mapper.selectPage(page, queryWrapper);

        return new PageResult<Checkitem>(checkitemIPage);

    }

    // 查找全部
    @Override
    public List<Checkitem> findAll() {
        List<Checkitem> checkitems = mapper.selectList(null);
        return checkitems;
    }

}
